package com.itwn.comic.web;

import com.github.pagehelper.PageInfo;
import com.itwn.comic.entity.Permission;
import com.itwn.comic.reult.ResponseResult;
import com.itwn.comic.service.PermissionService;
import com.itwn.comic.vo.PermissionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("permission")
public class PermissionController {
    
    @Autowired
    PermissionService permissionService;


    @GetMapping("/initMenu")
    public ResponseResult<List<PermissionVo>> initMenu(long id){
        ResponseResult<List<PermissionVo>> result=new ResponseResult<>();
        try{
            List<PermissionVo> list=permissionService.initLoadAllPermissionByPermissionId(id);
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(list);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败");
        }
        return result;
    }

    @GetMapping("/initAllMenu")
    public ResponseResult<List<PermissionVo>> initAllMenu(){
        ResponseResult<List<PermissionVo>> result=new ResponseResult<>();
        try{
            List<PermissionVo> list=permissionService.initLoadAllPermission();
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(list);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败");
        }
        return result;
    }
    
    @GetMapping("/list")
    public ResponseResult<PageInfo<PermissionVo>> list(@RequestParam(value = "now",defaultValue = "1") Integer now,
                                                       @RequestParam(value = "size",defaultValue = "3") Integer size, String name){
        ResponseResult<PageInfo<PermissionVo>> result=new ResponseResult<>();
        try{
            PageInfo<PermissionVo> pageInfo=this.permissionService.listByPage(now, size, name);
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询权限失败");
        }
        return result;
    }
    
    @GetMapping("/initParentMenu")
    public ResponseResult<List<PermissionVo>> initParentMenu(){
        ResponseResult<List<PermissionVo>> result=new ResponseResult<>();
        try{
            List<PermissionVo> list = this.permissionService.initAllByParentMenu();
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(list);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败！");
        }
        return result;
    }
    
    @PostMapping("/modify")
    public ResponseResult<Void> modify(@RequestBody Permission permission){
        ResponseResult<Void> result=new ResponseResult<>();
        permission.setUpdateTime(new Date());
        try{
            this.permissionService.modify(permission);
            result.setCode(200);
            result.setMsg("OK");
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("修改失败");
        }
        return result;
    }

    @PostMapping("/add")
    public ResponseResult<Void> add(@RequestBody Permission permission){
        ResponseResult<Void> result=new ResponseResult<>();
        try{
            this.permissionService.add(permission);
            result.setCode(200);
            result.setMsg("OK");
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("添加失败");
        }
        return result;
    }

    @DeleteMapping("/del/{id}")
    public ResponseResult<Void> remove(@PathVariable("id") long id){
        ResponseResult<Void> result=new ResponseResult<>();
        try{
            this.permissionService.remove(id);
            result.setCode(200);
            result.setMsg("OK");
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("删除失败");
        }
        return result;
    }

    @GetMapping("/initMenuByRole/{id}")
    public ResponseResult<List<Integer>> initMenuByRole(@PathVariable("id") int id){
        ResponseResult<List<Integer>> result = new ResponseResult<>();
        try {
            List<Integer> list = this.permissionService.queryMenuByRole(id);
            result.setCode(200);
            result.setMsg("查询成功!");
            result.setData(list);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败!");
        }
        return result;

    }
    
}
